'''
kook和discord机器人版本代码，目前还不会封装，就不献丑了
'''
# 离线 命令行版本查询数据

import numpy as np

# ANSI 转义码定义
RESET = "\033[0m"  # 重置所有属性
# 前景色
BLACK = "\033[30m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
WHITE = "\033[37m"

side_eng_to_cn = {
    '': '',
    'PLA': '中国(PLA)',
    'PLANMC': '中国海军陆战队(PLANMC)',
    'USA': '美国(USA)',
    'USMC': '美国海军陆战队(USMC)',
    'RUS': '俄罗斯(RUS)',
    'VDV': '俄罗斯空降兵(VDV)',
    'GB': '英国(GB)',
    'CAF': '加拿大(CAF)',
    'AUS': '澳大利亚(AUS)',
    'INS': '老乡(INS)',
    'MEA': '中东联盟(MEA)',
    'IMF': '民兵(IMF)'
}

# 因为html元素的id不是两个词，所以如果地图是两个词的话，所以要转换一下
map_dict = {
    'Al Barsrah': 'AlBasrah',
    'Fool\'s Road': 'FoolsRoad',
    'Goose Bay': 'GooseBay',
    'Kamdesh Highlands': 'Kamdesh',
    'Kohat Toi': 'Kohat',
    'Lashkar Valley': 'Lashkar',
    'Logar Valley': 'Logar',
    'Sumari Bala': 'Sumari',
    'Tallil Outskirts': 'Tallil',
    'Black Coast': 'BlackCoast'
}

map_complete = {
    'Anvil', 'Al Barsrah', 'Belaya', 'Chora', 'Fallujah', 'Fool\'s Road',
    'Goose Bay', 'Gorodok', 'Kamdesh Highlands', 'Kohat Toi', 'Kokan',
    'Lashkar Valley', 'Logar Valley', 'Manicouagan', 'Mestia', 'Mutaha',
    'Narva', 'Skorpo', 'Sumari Bala', 'Tallil Outskirts', 'Yehorivka',
    'Black Coast', 'Harju'
}
mode_complete = {'RAAS', 'AAS', 'Invasion', 'Seed', 'TC'}  # 还未支持Tank模式
team_complete = {
    'PLA', 'PLANMC', 'USA', 'USMC', 'RUS', 'VDV', 'GB', 'CAF', 'AUS', 'INS',
    'MEA', 'IMF'
}


def help():

  s = f'/q 查询 /help 帮助\n目前Tank模式还不支持\n查询查询格式为**(不区分大小写)**: /q 当前地图前三个英文字母 模式首字母' \
      f'第几张地图 下张地图前三个英文字母 模式首字母  第几张地图 当前阵营英语缩写\n' \
      f'比如说这场比赛Narva RAAS v4, 下场比赛Mutaha RAAS v3, 你现在是加军\n' \
      f'就输入: !q nar r 4 mut r 3 caf\n'
  s += '以下是阵容英文缩写对照表：\n'
  for i, key in enumerate(side_eng_to_cn):
    if i == 0:
      continue
    s += f'{i} {side_eng_to_cn[key]}\n'
  return s


# 处理两个单词地图的情况(比如：Talil Outskirts)
def parse_input(c: list):
  map1, mode1, version1, map2, mode2, version2 = '', '', '', '', '', ''
  if len(c) == 7:  # 正常情况，两张地图都是一个词
    map1, mode1, version1, map2, mode2, version2 = c[0], c[1], c[2], c[3], c[
        4], c[5]
  elif len(c) == 8:  # 一张地图两个单词，一张地图一个单词
    if c[1] not in ['RAAS', 'AAS', 'Invasion', 'Seed', 'Tanks', 'Seed',
                    'TC']:  # 第一张地图两个单词
      map1, mode1, version1, map2, mode2, version2 = map_dict[
          c[0] + ' ' + c[1]], c[2], c[3], c[4], c[5], c[6]
    else:  # 第二张地图两个单词
      map1, mode1, version1, map2, mode2, version2 = c[0], c[1], c[
          2], map_dict[c[3] + ' ' + c[4]], c[5], c[6]
  else:  # 两张地图都是两个词
    assert len(c) == 9
    map1, mode1, version1, map2, mode2, version2 = map_dict[
        c[0] + ' ' + c[1]], c[2], c[3], map_dict[c[4] + ' ' + c[5]], c[6], c[7]

  return map1, mode1, version1, map2, mode2, version2, c[-1]


# 更新本场比赛地图
def update_now_map(map_arg, mode_arg, version_arg):
  version = int(version_arg)
  special_zero = ''  # 因为他妈的如果地图大于10，就会变成0几，所以要特判某些地图
  if mode_arg == 'RAAS' and version < 10:  # 只有RAAS会地图大于10，其他模式目前还没大于10的
    if map_arg == 'Gorodok' or map_arg == 'Kohat' or map_arg == 'Yehorivka':
      special_zero = '0'

  file_path = './data/' + map_arg + '/' + mode_arg + ' v' + special_zero + str(
      version) + '/car_info.npy'
  dict = np.load(file_path, allow_pickle=True).item()
  team1, team2 = list(dict.keys())[0], list(dict.keys())[1]
  return team1, team2


# 查询你所在下场比赛地图阵营
def query_next_map(map_arg, mode_arg, version_arg, side_arg):
  s = ''
  version = int(version_arg)
  special_zero = ''  # 因为他妈的如果地图大于10，就会变成0几，所以要特判某些地图
  if mode_arg == 'RAAS' and version < 10:  # 只有RAAS会地图大于10，其他模式目前还没大于10的
    if map_arg == 'Gorodok' or map_arg == 'Kohat' or map_arg == 'Yehorivka':
      special_zero = '0'

  file_path = './data/' + map_arg + '/' + mode_arg + ' v' + special_zero + str(
      version) + '/car_info.npy'

  dict = np.load(file_path, allow_pickle=True).item()
  team1, team2 = list(dict.keys())[0], list(dict.keys())[1]

  team1_info, team2_info = [], []  # 两队载具信息，里面每个元素都是字符串

  # print('---------------')
  if side_arg == 1:  # 第一队
    team1_info.append(team1)
    for value in dict[team1]:
      team1_info.append(value)
    team2_info.append(team2)
    for value in dict[team2]:
      team2_info.append(value)

  else:  # 第二队
    assert side_arg == 2  # 第二队
    team1_info.append(team2)
    for value in dict[team2]:
      team1_info.append(value)
    team2_info.append(team1)
    for value in dict[team1]:
      team2_info.append(value)
  return team1_info, team2_info


# 对输入进行补全
def complete_input(map1_arg, model1_arg, version1_arg, map2_arg, model2_arg,
                   version2_arg, team_arg):
  res = []
  # 下面7个代码块分别是对7个输入参数进行补全
  ava = [
      option for option in map_complete
      if option.lower().startswith(map1_arg.lower())
  ]
  tmp = ava[0].split()
  for it in tmp:  # 解决一个地图多个词的情况
    res.append(it)

  ava = [
      option for option in mode_complete
      if option.lower().startswith(model1_arg.lower())
  ]
  res.append(ava[0])

  res.append(version1_arg)

  ava = [
      option for option in map_complete
      if option.lower().startswith(map2_arg.lower())
  ]
  tmp = ava[0].split()
  for it in tmp:
    res.append(it)

  ava = [
      option for option in mode_complete
      if option.lower().startswith(model2_arg.lower())
  ]
  res.append(ava[0])

  res.append(version2_arg)

  ava = [
      option for option in team_complete
      if option.lower() == team_arg.lower() # PLANMC和PLA 会出现bug
  ]
  res.append(ava[0])

  return res


def work(map1_arg, model1_arg, version1_arg, map2_arg, model2_arg,
         version2_arg, team_arg):

  # 补全输入信息
  # print(map1_arg, model1_arg, version1_arg, map2_arg, model2_arg, version2_arg, team_arg)
  completed_input = complete_input(map1_arg, model1_arg, version1_arg,
                                   map2_arg, model2_arg, version2_arg,
                                   team_arg)
  # print(completed_input)
  # 解析补全后的输入
  map1_arg, model1_arg, version1_arg, map2_arg, model2_arg, version2_arg, team_arg = parse_input(
      completed_input)

  # print(map1_arg, model1_arg, version1_arg, map2_arg, model2_arg, version2_arg, team_arg)

  team1, team2 = update_now_map(map1_arg, model1_arg, version1_arg)

  #当前和下一张地图信息，用于后面发送卡片信息
  map_info = f'当前地图 {map1_arg} {model1_arg} v{version1_arg} 你在 ' \
             f'{team_arg} 阵营  下一张地图 {map2_arg} {model2_arg} v{version2_arg}'
  now_side = team_arg
  next_side = -1  # 下场比赛是哪边阵营
  # print(now_side, team1, team2)
  if now_side == team1:
    next_side = 2
  elif now_side == team2:
    next_side = 1
  # print(map_info, query_next_map(map2_arg, model2_arg, version2_arg, next_side))
  return map_info, query_next_map(map2_arg, model2_arg, version2_arg,
                                  next_side)


if __name__ == '__main__':
    pass
    # s = 'mut r 7 tal r 4 pla'
    # a = s.split()
    # tmp1, tmp2 = work(a[0], a[1], a[2], a[3], a[4], a[5], a[6])
    # t1, t2 = tmp2
    # print(t1)


  # while True:
  #   # help()
  #   a = input().split()
  #   try:
  #     work(a[0], a[1], a[2], a[3], a[4], a[5], a[6])
  #   except Exception as e:
  #     print('出错了 :', e)
